package com.doordu.framework.cloud.xxl;

import com.doordu.framework.cloud.xxl.manager.ExecutorManager;
import com.doordu.framework.cloud.xxl.registry.DoorduJobAutoRegistration;
import com.doordu.framework.cloud.xxl.registry.DoorduJobRegistration;
import com.doordu.framework.cloud.xxl.registry.DoorduJobRegistry;
import com.xxl.job.core.executor.XxlJobExecutor;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * xxl-job自动配置
 *
 * @author zxw
 * @date 2020/9/21 14:00
 */
@Configuration
@EnableConfigurationProperties
@ConditionalOnProperty(
        value = {"doordu.job.enabled"},
        matchIfMissing = true
)
@ConditionalOnXxlJobEnabled
public class DoorduJobAutoConfiguration {
    private ConfigurableEnvironment env;

    public DoorduJobAutoConfiguration(ConfigurableEnvironment env) {
        this.env = env;
    }

    @Bean
    public DoorduJobInstanceConfigBean doorduJobInstanceConfigBean() {
        DoorduJobInstanceConfigBean doorduJobInstanceConfigBean = new DoorduJobInstanceConfigBean();
        String adminAddresses = getProperty("doordu.job.adminAddresses");
        String accessToken = getProperty("doordu.job.accessToken");
        String appname = getProperty("doordu.job.appname");
        String address = getProperty("doordu.job.address");
        String ip = getProperty("doordu.job.ip");
        int port = 0;
        int logRetentionDays = 0;
        String portString = getProperty("doordu.job.port");
        String logPath = getProperty("doordu.job.logPath");
        String daysString = getProperty("doordu.job.logRetentionDays");
        if(StringUtils.isNotBlank(ip)){
            port = Integer.parseInt(portString);
        }
        if(StringUtils.isNotBlank(daysString)){
            logRetentionDays = Integer.parseInt(daysString);
        }
        doorduJobInstanceConfigBean.setAccessToken(accessToken);
        doorduJobInstanceConfigBean.setAddress(address);
        doorduJobInstanceConfigBean.setAdminAddresses(adminAddresses);
        doorduJobInstanceConfigBean.setAppname(appname);
        doorduJobInstanceConfigBean.setIp(ip);
        doorduJobInstanceConfigBean.setLogPath(logPath);
        doorduJobInstanceConfigBean.setLogRetentionDays(logRetentionDays);
        doorduJobInstanceConfigBean.setPort(port);
        return doorduJobInstanceConfigBean;
    }

    @Bean
    public DoorduJobRegistry doorduJobRegistry() {
        return new DoorduJobRegistry();
    }


    @Bean
    @ConditionalOnMissingBean(value = ExecutorManager.class, search = SearchStrategy.CURRENT)
    public ExecutorManager executorManager(DoorduJobInstanceConfig config) {
        XxlJobExecutor xxlJobExecutor = new DoorduJobExecutorFactory().create(config);
        return new ExecutorManager(xxlJobExecutor, config);
    }

    @Bean
    public DoorduJobRegistration doorduJobRegistration(DoorduJobInstanceConfig doorduJobInstanceConfig, ExecutorManager executorManager) {
        return DoorduJobRegistration.builder(doorduJobInstanceConfig).with(executorManager).build();
    }


    @Bean
    public DoorduJobAutoRegistration doorduJobAutoRegistration(ApplicationContext context, DoorduJobRegistry doorduJobRegistry, DoorduJobRegistration doorduJobRegistration) {
        return new DoorduJobAutoRegistration(context, doorduJobRegistry, doorduJobRegistration);
    }

    private String getProperty(String property) {
        return this.env.containsProperty(property) ? this.env.getProperty(property) : "";
    }
}
